<template>
  <div class="tenantMenu_main main_container add_container">
    <div v-loading="loading" class="main_main">
      <div class="SearchBar_main main_main_search">
        <div class="waitOutNum">待出库数量：{{ waitOutNum }}</div>
        <div class="main_main_search_item">
          <el-input v-model="pageData.searchValue" style="width:200px;margin-right:10px" placeholder="请输入批号" clearable @keyup.enter.native="Search" @clear="SearchList" />
          <el-button style="background:#387bfc;width:30px;" icon="el-icon-search" @click.stop="Search" />
        </div>
      </div>
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          @sort-change="
            (column) => {
              sortChange(column, this);
            }
          "
          @selection-change="
            (val) => {
              selectionChange(val, this);
            }
          "
        >
          <Column
            v-for="(column,index) in defaultTableColumns"
            :key="index"
            :prop="column.prop"
            :is-show="column.isShow"
            :label="column.label"
            :sortable="column.sortable"
            :width="column.width"
          />
          <el-table-column
            prop="outStockNum"
            label="出库数量"
            min-width="120"
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.outStockNum" placeholder="请输入" clearable />
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--    <div class="main_bottoms">-->
    <!--      <pagination-->
    <!--        :total="total"-->
    <!--        :page.sync="pageData.pageNum"-->
    <!--        :limit.sync="pageData.pageSize"-->
    <!--        :selected-num="selectionsAll.length"-->
    <!--        @pagination="getList"-->
    <!--      />-->
    <!--    </div>-->
    <div class="main_bottom">
      <div>
        <el-button type="cancel" @click.stop="cancel">取消</el-button>
        <el-button @click.stop="onSubmit">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getMaterialInfo2 } from '@/api/warehouseManagement/warehouseInventory'
export default {
  name: 'Index',
  props: {
    layerid: {},
    selectedRow: {},
    materialId: {},
    isOutWarehouseNum: {},
    waitOutNum: {}
  },
  data() {
    return {
      loading: true,
      pageId: '库存明细', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        searchValue: '',
        status: 1
      },
      loadoutStockNum: 0, // 出库数量总和
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      isAll: '0',
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'palletBatchCode', label: '批号' },
        { prop: 'productDate', label: '生产日期' },
        { prop: 'warehouseName', label: '仓库' },
        { prop: 'locationName', label: '库位' },
        { prop: 'stockNum', label: '库存数量' },
        { prop: 'productCountDown', label: '到期倒计时(天)' }
      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'code', label: '批号', type: 'input' }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: '' },
        { label: '批量删除', perm: '' },
        { label: '批量导出', perm: '' },
        { label: '页面配置', perm: '' }
      ],
      tableDataOne: [],
      tableList: []
    }
  },
  mounted() {
    this.getList()
    this.$nextTick(() => {
      this.tableHeight = this.$refs.table_container.offsetHeight
    })
  },
  methods: {
    getList() {
      if (this.isOutWarehouseNum) {
        getMaterialInfo2(this.materialId, this.isAll).then(res => {
          const cacheTableData = this.$deepClone(res.data.details)
          this.tableDataOne = this.$deepClone(res.data.details)
          if (cacheTableData.length == 1) {
            cacheTableData.filter(item => {
              item.outStockNum = this.waitOutNum
            })
          }
          cacheTableData.forEach(item => {
            if (this.selectedRow && this.selectedRow.length > 0 && this.selectedRow.some(item2 => item2.outStockNum)) {
              this.selectedRow.forEach(item2 => {
                if (item.id == item2.stockDetailsId) {
                  item.outStockNum = item2.outStockNum
                }
              })
            }
          })
          this.tableData = cacheTableData
          this.tableList = cacheTableData
          this.loading = false
        })
      }
    },

    Search() {
      this.tableDataOne.forEach(item => {
        if (!item.palletBatchCode) {
          item.palletBatchCode = ''
        }
      })
      this.tableData = this.tableDataOne.filter(item => item.palletBatchCode.indexOf(this.pageData.searchValue) !== -1)
      this.$nextTick(() => {
        this.tableData.forEach(ele => {
          this.$refs.table.toggleRowSelection(ele, true)
        })
      })
    },
    SearchList() {
      if (!this.pageData.searchValue) {
        this.tableData = this.tableDataOne
      }
    },
    onSubmit() {
      const tableData1 = JSON.parse(JSON.stringify(this.tableData))
      tableData1.map(item => {
        if (item.outStockNum) {
          this.loadoutStockNum += Number(item.outStockNum)
        } else {
          item.outStockNum = 0
          this.loadoutStockNum += Number(item.outStockNum)
        }
      })
      // this.tableData = tableData1
      if (tableData1.filter(item => Number(item.outStockNum) > Number(item.stockNum)).length > 0) {
        this.$message.warning('出库数量应该小于库存数量')
        this.loadoutStockNum = 0
        return
      } else if (tableData1.filter(item => item.outStockNum && !this.SETTINGS.reg_zero.test(item.outStockNum)).length > 0) {
        this.$message.warning('出库数量未正确填写')
        this.loadoutStockNum = 0
        return
      } else if (this.loadoutStockNum != this.waitOutNum) {
        this.$message.warning('出库数量总和需与待出库数量相同')
        this.loadoutStockNum = 0
        return
      }
      this.$parent.showWarehouse(tableData1)
      this.$layer.close(this.layerid)// 关闭弹框
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    }
  }
}
</script>

<style lang="less" scoped>
.waitOutNum{
    height: 50px;
    padding-left: 20px;
    line-height: 64px;
    font-size: 14px;
    font-weight: 400;
    color: #666;
}
.main_bottoms{
  width: 100%;
  height: 50px;
  background-color: white;
  padding: 0px 30px;
  display: flex;justify-content:flex-end;align-items: center;
}
.formItem_header_operate_items{
  height: 30px;
  display: flex;justify-content: center;align-items: center;
  padding: 0px 10px;font-size: 16px;border-radius: 4px;
  color: rgba(56, 123, 252, 1);background-color: rgba(235, 242, 255, 1);
  .item_name{
    font-size: 14px;padding-left: 10px;
  }
}
.formItem_header_operate_items:hover{
  background-color: rgba(235, 242, 255, 0.5);
}
.main_bottom{
  padding-right: 20px !important;
}
.main_search_item{
  padding-right: 20px !important;
}
.main_main_search_item{
  padding-left: 20px !important;
}
.main_main{
  margin: 0 !important;
  padding-top:0 !important ;
}
.el-input__inner {
  text-align: left;
}
/deep/ .el-input__inner{
  text-align: left;
}
/**
 * 解决el-input设置类型为number时，中文输入法光标上移问题
 **/
/deep/.el-input__inner{
  line-height: 1px !important;
}

/**
 * 解决el-input设置类型为number时，去掉输入框后面上下箭头
 **/
/deep/ input[type="number"]::-webkit-inner-spin-button, input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
::v-deep .el-input__suffix {
  .el-icon-circle-close:before {
    padding-top: 5px;
  }
}
</style>
